package cn.scu.ne04jdemo.controller;


import cn.scu.ne04jdemo.entitiy.EarningRightChangeEntity;
import cn.scu.ne04jdemo.entitiy.OwnershipRightChangeEntity;
import cn.scu.ne04jdemo.service.EarningRightChangeService;
import cn.scu.ne04jdemo.service.OwnershipRightChangeService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/earning/right/change")
public class EarningRightChangeController {

    @Autowired
    private EarningRightChangeService earningRightChangeService;


    @GetMapping("/findPage")
    public IPage<EarningRightChangeEntity> findPage(@RequestParam String userNameParam
            , @RequestParam(required = true) long current, @RequestParam(required = true)  long size){
        QueryWrapper<EarningRightChangeEntity> params = new QueryWrapper();
        params.like(StringUtils.isNotBlank(userNameParam), "user_name", userNameParam);
        IPage<EarningRightChangeEntity> page = new Page<EarningRightChangeEntity>(current, size);
        try {
            page = earningRightChangeService.page(page, params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return page;
    }



    @PostMapping("/delById")
    public Boolean delById(@RequestParam(required = true) Integer id){
        try {
            earningRightChangeService.removeById(id);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @PostMapping("/save")
    @ResponseBody
    public Boolean save(@RequestBody EarningRightChangeEntity earningRightChangeEntity){
        try {
            if(!ObjectUtils.isNotNull(earningRightChangeEntity))
                earningRightChangeEntity = new EarningRightChangeEntity();
            earningRightChangeService.save(earningRightChangeEntity);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @GetMapping("/download")
    @ResponseBody
    public void download(@RequestParam String userNameParam, HttpServletResponse response) {
        QueryWrapper<EarningRightChangeEntity> params = new QueryWrapper();
        params.like(StringUtils.isNotBlank(userNameParam), "user_name", userNameParam);
        List<EarningRightChangeEntity> earningRightChangeEntityList =  earningRightChangeService.list(params);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("收益权变更.xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            EasyExcel.write(response.getOutputStream(), EarningRightChangeEntity.class).sheet().doWrite(earningRightChangeEntityList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
